package com.whut.blog.service.impl;
//提供登录注册相关的服务
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whut.blog.entity.UserT;
import com.whut.blog.service.IUserSQL;
//import com.whut.blog.Entity.UserEntity;
import com.whut.blog.service.IUserService;

import jakarta.annotation.Resource;

// public interface IUserService {
//     public boolean userLogin(UserEntity user);
// }

@Service
public class UserService implements IUserService{

    @Resource
    private IUserSQL userSQL;
    public boolean userLogin(String username,String password){
        
        //构造sql查询条件
        QueryWrapper<UserT> wrapper = new QueryWrapper<>();
        wrapper.select("name","password").eq("name", username);
        //执行查询
        List<UserT> res = userSQL.list(wrapper);
        if (res.size() == 0){//用户不存在
            return false;
        }
        else if(res.get(0).getPassword().equals(password)){//密码正确
            return true;
        }
        else{//密码错误
            return false;
        }
       
    }

    public boolean userRegister(MultipartFile avatar,String name,String password, String email,String birthday){
        
        //构造sql查询条件
        QueryWrapper<UserT> wrapper = new QueryWrapper<>();
        wrapper.select("name","email").eq("name", name);
        //执行查询
        List<UserT> res = userSQL.list(wrapper);
        if (res.size() > 0){
            return false;
        }
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" ); //格式化日期
        //创建图片保存目录
        File dir = new File("img");
        if(!dir.isDirectory()){
            dir.mkdirs();
        }
        //图片名=日期+UUID+原始图片名
        String fname =  sdf.format(new Date())+UUID.randomUUID().toString()+avatar.getOriginalFilename();
        System.out.println(fname);

        //保存图片文件
        try {
            avatar.transferTo(new File(dir,fname).getAbsoluteFile()); // 要用绝对路径！！
            System.out.println(new File(dir,fname).getAbsolutePath());//输出（上传文件）保存的绝对路径
        }
        catch (IOException e){
            e.printStackTrace();
        }
        UserT us = new UserT(); //创建一个实体
        us.setName(name);
        us.setPassword(password);
        us.setEmail(email);
        us.setBirthday(birthday);
        us.setAvatar(new File(dir,fname).getAbsolutePath());
        userSQL.save(us);
        return true;
    }

    public boolean updatePwd(String name,String password,String newpwd){
        //构造sql查询条件
        QueryWrapper<UserT> wrapper = new QueryWrapper<>();
        wrapper.select("name","password").eq("name", name);
        //执行查询
        List<UserT> res = userSQL.list(wrapper);
        if (res.size() == 0){//用户不存在
            return false;
        }
        else if(res.get(0).getPassword().equals(password)){//密码正确
            UserT us = new UserT();
            us.setPassword(newpwd);
            userSQL.update(us,wrapper);
            return true;
        }
        else{//密码错误
            return false;
        }
    }

}
